package com.binge.test;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class DataProcessor {
    public static void main(String[] args) {
        String jsonFilePath = "input.json";
        String targetFilePath = "/Users/bin/Library/Containers/com.tencent.WeWorkMac/Data/Documents/Profiles/A23F871921DD6C41602D2DBB1208FFD6/Caches/Files/2024-04/8ea5dd6bb3ceb5300156070638e36b32/prd-podip.yaml";

        try {
            // 读取JSON文件
            String jsonContent = readFile(jsonFilePath);

            // 解析JSON数据
            Gson gson = new Gson();
            JsonArray jsonArray = gson.fromJson(jsonContent, JsonArray.class);

            // 遍历JSON数据
            for (JsonElement element : jsonArray) {
                JsonObject item = element.getAsJsonObject();
                String key = item.get("key").getAsString();
                int docCount = item.get("doc_count").getAsInt();

                // 过滤目标文件
                String filteredLine = filterTargetFile(targetFilePath, key);

                if (!filteredLine.isEmpty()) {
                    // 提取第二列的值
                    String secondColumn = filteredLine.split("\\s+")[1];

                    // 拼接结果
                    String result = secondColumn + "-" + docCount;
                    System.out.println(result);

                    // 写入文件

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 读取文件内容
    private static String readFile(String filePath) throws IOException {
        StringBuilder content = new StringBuilder();
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        String line;
        while ((line = reader.readLine()) != null) {
            content.append(line);
        }
        reader.close();
        return content.toString();
    }

    // 过滤目标文件
    private static String filterTargetFile(String filePath, String key) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.contains(key)) {
                reader.close();
                return line;
            }
        }
        reader.close();
        return "";
    }
}